package com.zyj.exam_app.user_manager.controller;

import com.zyj.exam_app.user_manager.bean.UserBean;
import com.zyj.exam_app.user_manager.mapper.UserMapper;
import com.zyj.exam_app.user_manager.service.UserManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/userManager")
public class UserManagerController {

//    @Autowired
//    private UserMapper userMapper;

    @Autowired
    private UserManagerService userManagerService;



    @RequestMapping("/userList")
    public String userList(Model model){
        //查询所有用户信息
        List<UserBean> userList = userManagerService.getAllUserInfo();
        model.addAttribute("userList", userList);
        return "user/list";
    }

    @GetMapping("/userAddPage")
    public String UserAddPage(){
        return "user/add";
    }

    @PostMapping("/addOneUser")
    public String addOneUser(UserBean userInfo){
        System.out.println(userInfo);
        userManagerService.addOneUser(userInfo);
        return "forward:/userManager/userList";
    }

    @GetMapping("/deleteOneUser/{userId}")
    public String deleteOneUser(@PathVariable("userId") String userId){
        System.out.println("删除用户: " + userId);
        userManagerService.deleteOneUser(userId);
        return "forward:/userManager/userList";
    }

    @GetMapping("/userView/{userId}")
    public String userView(@PathVariable("userId") String userId, Model model) {
        UserBean user = userManagerService.findUserById(userId);
        model.addAttribute("user", user);
        return "user/userView";
    }

//    @RequestMapping(method = RequestMethod.POST, value = "/doLogin")
//    @ResponseBody
//    public String doLogin(UserBean user){
//        //登录验证
//        System.out.println(user.getAccount());
//        System.out.println(user.getPassword());
//        //获取菜单
////        List<MenuBean> menus = MenuFactory.createTestMenus();
////        model.addAttribute("menus", menus);
//        Map<String, Object> result = new HashMap<>();
//        result.put("success", true);
//        return "index";
//    }



}
